﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using InkFx.WinControl.Core;

namespace Temp_20190707_AForm
{
    public partial class DockFloatForm : Form
    {
        public DockFloatForm()
        {
            InitializeComponent();
            base.DoubleBuffered = true;
        }

        public static DockFloatForm ShowControl(DockPanel dockPanel, Control control, Rectangle rect)
        {
            DockFloatForm form = new DockFloatForm();
            form.StartPosition = FormStartPosition.Manual;
            form.Control = control;
            form.DockPanel = dockPanel;

            if (rect != Rectangle.Empty) form.Bounds = new Rectangle(rect.X - 30, rect.Y - 10, rect.Width, rect.Height);

            control.Dock = DockStyle.Fill;
            form.Controls.Add(control);
            form.Show(dockPanel.FindForm());

            //form.Activate();

            //选中新窗体, 并且执行拖动
            Win32API.ReleaseCapture();
            Win32API.SendMessage(form.Handle, Win32Msg.WM_SYSCOMMAND, Win32Msg.SC_MOVE + Win32Msg.HTCAPTION, 0);
            if (Control.MouseButtons == MouseButtons.Left)
            {
                //form.OnNCMouseDown();
                Win32API.SendMessage(form.Handle, Win32Msg.WM_NCLBUTTONDOWN, 0, 0);
            }
            return form;
        }

        public DockTipForm Form2 { get; set; }
        public Control Control { get; set; }
        public DockPanel DockPanel { get; set; }



        public void StartMove()
        {
            if (!this.Visible) this.Visible = true;

            //Point screenPoint = Cursor.Position;
            //int OFFSET_PX = panTitle.Height / 2;

            //if (DragOffsetPoint == Point.Empty) DragOffsetPoint = new Point(OFFSET_PX, OFFSET_PX);
            //Point locaPoint = new Point(screenPoint.X - DragOffsetPoint.X, screenPoint.Y - DragOffsetPoint.Y);
            //if (this.Location != locaPoint) this.Location = locaPoint;

            ControlHelper.DoControlMouseDown(this);
            if (!Visible) this.Visible = true;
        }


        protected override void WndProc(ref Message m)
        {
            const int WM_NCLBUTTONDOWN = 0xA1;
            const int WM_NCLBUTTONUP = 0x00A2;


            base.WndProc(ref m);

            if (m.Msg == WM_NCLBUTTONDOWN)      OnNCMouseDown();
            else if (m.Msg == WM_NCLBUTTONUP)   OnNCMouseUp();  //WM_NCLBUTTONUP 消息没用
        }



        private long m_Timer = 0;
        private Point m_StartDockPoint = Point.Empty;
        protected void OnNCMouseDown()
        {
            //MessageBox.Show("OnMouseDown");
            if (m_Timer >= 1) Task.ClearInterval(m_Timer);
            m_Timer = Task.SetInterval(() =>
            {
                Invoke((ThreadStart) (() =>
                {
                    //if (m_StartDockPoint != Point.Empty)
                    {
                        if (Control.MouseButtons == MouseButtons.None)
                        {
                            m_StartDockPoint = Point.Empty;
                            Task.ClearInterval(m_Timer);
                            OnNCMouseUp();
                        }
                    }
                }));
            }, 50);
            m_StartDockPoint = Control.MousePosition;
        }
        protected void OnNCMouseUp()
        {
            //MessageBox.Show("OnMouseUp");

            var dockPanel = DockPanel;
            DockPanel.DoDock(Control);
            dockPanel.HideDLPanel();
        }

        protected override void OnLocationChanged(EventArgs e)
        {
            base.OnLocationChanged(e);
            if (m_StartDockPoint != Control.MousePosition)
            {
                DockPanel.ShowDLPanel();
            }
        }



        public new void Dispose()
        {
            this.Close();
            if (m_Timer >= 1) Task.ClearInterval(m_Timer);
            Form2 = null;
            Control = null;
            DockPanel = null;
            base.Dispose();
        }



    }
}
